import { getCloseZhWord } from "./getCloseWord";
import { ICLoseBusType, IPageType, ValidResType } from "./interface";
import { showConfirm } from "./showConfirm";

/**
 * 发起按钮跳转前校验,校验成功后navigate到新建的路由
 * @param param0
 * @returns
 */
export const handleCancelValid = async ({
  busType,
  record = {},
  type,
  busZhName = "合同",
  validResRequest,
  orderType = 4,
}: {
  busType: ICLoseBusType;
  type: IPageType;
  record: any | undefined | null;
  busZhName?: string;
  validResRequest: (p: { data: any }) => Promise<ValidResType>;
  orderType?: any;
}) => {
  if (!record) {
    return;
  }
  const busTitle = busZhName + getCloseZhWord({ busType });

  try {
    const validRes: ValidResType = await validResRequest({
      data: {
        ...record,
        orderId: record.id,
        handleType: 2,
        id: undefined,
        orderType: orderType,
        type,
        busType,
      },
    });

    if (!validRes || !validRes.pass) {
      showConfirm({
        title: busTitle,
        content: validRes.message,
        okText: "知道了",
        cancelButtonProps: {
          style: {
            display: "none",
          },
        },
      });

      return Promise.reject();
    } else {
      return Promise.resolve(validRes);
    }
  } catch (error) {
    return Promise.reject();
  }
};
